﻿@typeparam TItem
@using Blazorise;
@using Blazorise.DataGrid;
@using Microsoft.JSInterop
@using System.Reflection
@using Volo.Abp.Data
@using Volo.Abp.BlazoriseUI.Components.ObjectExtending
@using Volo.Abp.BlazoriseUI.Components
@inject IJSRuntime JsRuntime
@inject IIdGenerator IdGenerator

<DataGrid ElementId="@ElementId" TItem="TItem"
          Data="@Data"
          ReadData="@ReadData"
          TotalItems="@TotalItems"
          ShowPager="@ShowPager"
          CurrentPage="@CurrentPage"
          PageSize="@PageSize"
          Resizable="@Resizable"
          SelectionMode="@SelectionMode"
          @bind-SelectedRow="@SelectedItem"
          @bind-SelectedRows="@SelectedItems"
          Responsive
          FixedHeader
          FixedHeaderDataGridHeight="@DataGridHeight"
          FixedHeaderDataGridMaxHeight="@DataGridHeight"
          >
    <FirstPageButtonTemplate>
        <Icon Name="IconName.StepBackward"></Icon>
    </FirstPageButtonTemplate>
    <LastPageButtonTemplate>        
        <Icon Name="IconName.StepForward"></Icon>
    </LastPageButtonTemplate>
    <PreviousPageButtonTemplate>
        <Icon Name="IconName.ChevronLeft"></Icon>
        </PreviousPageButtonTemplate>
    <NextPageButtonTemplate>
        <Icon Name="IconName.ChevronRight"></Icon>
        </NextPageButtonTemplate>
    <LoadingTemplate>
        <Row Class="w-100 align-items-center" Style="height: 150px;">
            <Column>
               <RadarSpinner />
            </Column>
        </Row>
    </LoadingTemplate>
    <EmptyTemplate>
        <Row Class="w-100 align-items-center" Style="height: 150px;">
            <Column>
                <Heading Size="HeadingSize.Is4" TextAlignment="TextAlignment.Center">No data available</Heading>
            </Column>
        </Row>
    </EmptyTemplate>
    <DataGridColumns>
        @if (SelectionMode == DataGridSelectionMode.Multiple)
        {
            <DataGridMultiSelectColumn TItem="TItem" Width="30px"></DataGridMultiSelectColumn>
        }
        @if (Columns != null)
        {
            @foreach (var column in Columns)
            {
                if (column.Actions.Any())
                {
                    <DataGridEntityActionsColumn TItem="TItem" @ref="ActionColumns[column.Title]" Caption="@column.Title" TextAlignment="TextAlignment.End" ShowCaption="false" Width="@null">
                        <DisplayTemplate>
                            <EntityActions Type="column.Actions.Count>1? ActionType.Dropdown : ActionType.Button" TItem="TItem" ToggleColor="Color.Light" EntityActionsColumn="ActionColumns[column.Title]">
                                @foreach (var action in column.Actions)
                                {
                                    if (action.ConfirmationMessage != null)
                                    {
                                        <EntityAction TItem="TItem" Primary="action.Primary"
                                                      Color="@(action.Color != null ? (Blazorise.Color) action.Color : Blazorise.Color.Light)"
                                                      Icon="@action.Icon"
                                                      Clicked="async () => await action.Clicked(context)"
                                                      ConfirmationMessage="() => action.ConfirmationMessage.Invoke(context)"
                                                      Visible="@(action.Visible != null ? action.Visible(context) : true)"
                                                      Text="@action.Text"
                                                      Disabled="@action.Disabled">
                                        </EntityAction>
                                    }
                                    else
                                    {
                                        <EntityAction TItem="TItem" Primary="action.Primary"
                                                      Clicked="async () => await action.Clicked(context)"
                                                      Color="@(action.Color != null ? (Blazorise.Color) action.Color : Blazorise.Color.Light)"
                                                      Icon="@action.Icon"
                                                      Visible="@(action.Visible != null ? action.Visible(context) : true)"
                                                      Text="@action.Text"
                                                      Disabled="@action.Disabled">
                                        </EntityAction>
                                    }
                                }
                            </EntityActions>
                        </DisplayTemplate>
                    </DataGridEntityActionsColumn>
                }
                else
                {
                    @if (column.Component != null)
                    {
                        <DataGridColumn TItem="TItem" Field="@typeof(TItem).GetProperties().First().Name" Caption="@column.Title">
                            <DisplayTemplate>
                                @RenderCustomTableColumnComponent(column.Component, context)
                            </DisplayTemplate>
                        </DataGridColumn>
                    }
                    else
                    {
                        if (!ExtensionPropertiesRegex.IsMatch(column.Data))
                        {
                            @if (column.ValueConverter == null)
                            {
                                var property = typeof(TItem).GetProperty(column.Data);
                                if (property.PropertyType == typeof(Boolean))
                                {
                                    <DataGridColumn TItem="TItem" Field="@column.Data" Caption="@column.Title" Sortable="@column.Sortable">
                                        <DisplayTemplate>
                                            @if ((bool)property.GetValue(context))
                                            {                                    
                                                <Icon class="text-success" Name="IconName.Check" />
                                            }
                                            else
                                            {
                                                <Icon class="text-danger" Name="IconName.Times" />
                                            }
                                        </DisplayTemplate>
                                    </DataGridColumn>                                    
                                }
                                else
                                {
                                    <DataGridColumn TItem="TItem"
                                                Field="@column.Data"
                                                Caption="@column.Title"
                                                Sortable="@column.Sortable"
                                                DisplayFormat="@column.DisplayFormat"
                                                DisplayFormatProvider="@column.DisplayFormatProvider"/>
                                }
                            }
                            else
                            {
                                <DataGridColumn TItem="TItem"
                                                Field="@column.Data"
                                                Caption="@column.Title"
                                                Sortable="@column.Sortable">
                                    <DisplayTemplate>
                                        @(GetConvertedFieldValue(context, column))
                                    </DisplayTemplate>
                                </DataGridColumn>
                            }
                        }
                        else
                        {
                            <DataGridColumn TItem="TItem" Field="@nameof(IHasExtraProperties.ExtraProperties)" Caption="@column.Title">
                                <DisplayTemplate>
                                    @{
                                        var entity = context as IHasExtraProperties;
                                        var propertyName = ExtensionPropertiesRegex.Match(column.Data).Groups[1].Value;
                                        var propertyValue = entity.GetProperty(propertyName);
                                        if (propertyValue != null && propertyValue.GetType() == typeof(bool))
                                        {
                                            if ((bool) propertyValue)
                                            {
                                                <Icon class="text-success" Name="IconName.Check"/>
                                            }
                                            else
                                            {
                                                <Icon class="text-danger" Name="IconName.Times"/>
                                            }
                                        }
                                        else
                                        {
                                            if (column.ValueConverter != null)
                                            {
                                                @(GetConvertedFieldValue(context, column))
                                            }
                                            else
                                            {
                                                if (column.DisplayFormat == null)
                                                {
                                                    @(propertyValue)
                                                }
                                                else
                                                {
                                                    @(string.Format(column.DisplayFormatProvider, column.DisplayFormat, propertyValue))
                                                }
                                            }
                                        }
                                    }
                                </DisplayTemplate>
                            </DataGridColumn>
                        }
                    }
                }
            }
        }
    </DataGridColumns>
</DataGrid>

